<template>
  <div class="container">
    <el-form ref="form" :model="form" :rules="rules" label-width="90px">
      <el-form-item label="账号:" prop="username">
        <el-input v-model="form.username"></el-input>
      </el-form-item>
      <el-form-item label="员工姓名:" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="手机号:" prop="phone">
        <el-input v-model="form.phone"></el-input>
      </el-form-item>
      <el-form-item label="性别:">
        <el-radio-group v-model="form.sex">
          <el-radio label="1">男</el-radio>
          <el-radio label="2">女</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="身份证号:" prop="idNumber">
        <el-input v-model="form.idNumber"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="$router.push({ name: 'Employee' })">取消</el-button>
        <el-button type="primary" @click="onSubmit">保存</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addEmployee, getEmployee, changeStatus } from "@/api/employee";
export default {
  computed: {
    isEdit() {
      return this.$route.query.isEdit === "true";
    },
    id() {
      return this.$route.query.id;
    },
  },
  created() {
    if (this.isEdit) {
      getEmployee(this.id).then((res) => {
        this.form = res.data;
      });
    }
  },
  data() {
    return {
      // 提交的数据
      form: {
        username: "",
        name: "",
        phone: "",
        sex: "1",
        idNumber: "",
      },
      // 验证规则对象
      rules: {
        username: [
          {
            required: true,
            message: "请输入账号",
            trigger: ["blur", "change"],
          },
          {
            min: 3,
            max: 20,
            message: "账号长度应是3-20",
            trigger: ["blur", "change"],
          },
        ],
        name: [
          {
            required: true,
            message: "请输入员工姓名",
            trigger: ["blur", "change"],
          },
        ],
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: ["blur", "change"],
          },
          {
            min: 11,
            max: 11,
            message: "请输入正确的手机号",
            trigger: ["blur", "change"],
          },
        ],
        idNumber: [
          {
            required: true,
            message: "请输入身份证号",
            trigger: ["blur", "change"],
          },
          {
            min: 18,
            max: 18,
            message: "身份证号码不正确",
            trigger: ["blur", "change"],
          },
        ],
      },
    };
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.isEdit) {
            await changeStatus(this.form);
            this.$message.success("编辑员工成功");
          } else {
            await addEmployee(this.form);
            this.$message.success("添加员工成功");
          }
          this.$router.push({ name: "Employee" });
        } else {
          return false;
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.container {
  padding: 55px;
  .el-form-item {
    width: 400px;
  }
}
</style>
